package com.smart.controller.sys;

import java.util.List;
import java.util.ArrayList;

import com.smart.util.DateUtil;
import com.smart.util.ExportExcelUtils;
import com.smart.util.Messages;

import io.swagger.annotations.Api;
import javax.annotation.Resource;
import com.smart.entity.SiteInspection;
import com.smart.vo.sys.SiteInspectionVo;
import io.swagger.annotations.ApiOperation;
import com.smart.controller.BaseController;
import javax.servlet.http.HttpServletResponse;
import com.smart.service.SiteInspectionService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author lgb
 * @date 2020年6月23日 上午9:19:31
 * Copyright (C) 深圳工勘科技
 * @说明: 后台端巡检
 **/
@RestController
@RequestMapping("/sys/site_inspection")
@Api(value="siteInspection",tags="智慧工地-巡检管理")
public class SysSiteInspectionController extends BaseController{

	@Resource
	private SiteInspectionService siteInspectionService;
	
	@ApiOperation(value="sys获取工地巡检分页",notes="sys获取工地巡检分页")
	@GetMapping(value="/page")
	public Messages<IPage<SiteInspection>> page(String submitName,
		String createTime_start,String createTime_end,Integer siteId,
			@RequestParam(name="pageNum")Integer pageNum,
			@RequestParam(name="pageSize")Integer pageSize){ 
		IPage<SiteInspection> page=
				siteInspectionService.sysFindPage(submitName, 
					createTime_start,createTime_end,siteId,pageNum,pageSize);
		return Messages.success(page);
	}
	
	@ApiOperation(value="sys获取工地巡检详情",notes="sys获取工地巡检详情")
	@GetMapping(value="/detail/{id}")
	public Messages<SiteInspection> detail(@PathVariable Integer id){ 
		SiteInspection sIns = siteInspectionService.findById(id);
		return Messages.success(sIns);
	}
	
	/**
	 * @param res
	 * @return 导出所有数据
	 **/
	@GetMapping(value="/export_excel")
    public void exportExcel(HttpServletResponse res){
		List<SiteInspection> list = siteInspectionService.findAll();
		List<SiteInspectionVo> voList = new ArrayList<>();
		if(!list.isEmpty()){
			for(SiteInspection ins : list){
				SiteInspectionVo vo = new SiteInspectionVo();
				vo.setCreateTime(DateUtil.formatLongStr(ins.getCreateTime()));
				vo.setInspectionDate(ins.getInspectionDate());
				vo.setPosition(ins.getPosition());
				vo.setRemark(ins.getRemark());
				vo.setResult(ins.getResult()==0?"异常":"正常");
				vo.setSiteName(ins.getSiteName());
				vo.setSubmitName(ins.getSubmitName());
				voList.add(vo);
			}
		}
		String title = "工地巡检记录.xlsx";
        String[] rowName={"提交时间","所属日期","所属工地","巡检位置","巡检结果","巡检内容","提交者"};
        List<Object> datalist=new ArrayList<>(1);
        datalist.addAll(voList);
        ExportExcelUtils.exportExcel(res,title,rowName,datalist);
	}
	
}